﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using System.Web.Script.Serialization;
using Register.Command;

namespace RegisterWeb.Manager.SystemUserManager.ashx
{
    /// <summary>
    /// AdminInfoManagerService 的摘要说明
    /// </summary>
    public class AdminInfoManagerService : IHttpHandler
    {

        private String GetHospitalInfo(String ID)
        {
            if (ID.Equals("00000"))
            {
                return "系统管理员";
            }
            else
            {
                return (HospitalInfoBLL.GetHospitalInfoByID(ID)).Hospital_Name;
            }

        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            String action = context.Request["action"].ToString();
            if (action.Equals("list"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<AdminInfo> list = new List<AdminInfo>();
                list = AdminInfoBLL.GetPagedAdminInfo(row, page, " 1=1 ");

                List<AdminInfoJSON> json = new List<AdminInfoJSON>();
                foreach (AdminInfo admin in list)
                {
                    json.Add(new AdminInfoJSON(admin.Administrator_Name, admin.Administrator_ID, admin.Administrator_Password, GetHospitalInfo(admin.Administrator_Hosptial), admin.Administrator_AddTime.ToString(), admin.Administrator_Recoder, admin.Administrator_LastRegTime.ToString(), admin.Administrator_Hosptial));
                }

                AdminInfoListJSON adminListJSON = new AdminInfoListJSON();
                adminListJSON.total = AdminInfoBLL.GetAdminInfoCount(null);
                adminListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(adminListJSON));
            }
            else if (action.Equals("delete"))
            {
                String ID = context.Request["id"];
                if (AdminInfoBLL.DeleteAdminInfo(ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            else if (action.Equals("add"))
            {
                AdminInfo admin = new AdminInfo();
                admin.Administrator_ID = DataIDHelper.GetDataID("Administrator_ID");
                admin.Administrator_Name = context.Request["textAdminName"];
                admin.Administrator_Password = MD5Provider.Hash(context.Request["textAdminPassword"]);
                admin.Administrator_Recoder = "admin";///////////////////////////////////////////////////////////////////////////记录
                admin.Administrator_AddTime = DateTime.Now;
                admin.Administrator_Hosptial = context.Request["selectHosptial"];
                admin.Administrator_LastRegTime = DateTime.Now;
                //if (AdminInfoBLL.AddAdminInfo(admin))
                //    context.Response.Write("ok");
                //else
                //    context.Response.Write("no");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(AdminInfoBLL.AddAdminInfo(admin), "用户添加成功！")));

            }
            else if (action.Equals("edit"))
            {
                AdminInfo admin = new AdminInfo();
                admin.Administrator_ID = context.Request["id"];
                admin.Administrator_Name = context.Request["textAdminName"];
                admin.Administrator_Password = MD5Provider.Hash(context.Request["textAdminPassword"]);
                admin.Administrator_Hosptial = context.Request["selectHosptial"];
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(AdminInfoBLL.UpdateAdminInfo(admin), "编辑用户成功！")));

            }
            else if (action.Equals("check"))
            {
                string name = context.Request["name"];
                if (AdminInfoBLL.Exists(name))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    //class message
    //{
    //    public message(bool success, string msg)
    //    {
    //        this.msg = msg;
    //        this.success = success;
    //    }
    //    public bool success { get; set; }
    //    public string msg { get; set; }
    //}

    class AdminInfoListJSON
    {

        public int total { get; set; }
        public List<AdminInfoJSON> rows { get; set; }

    }

    class AdminInfoJSON
    {
        public String Name { get; set; }
        public String ID { get; set; }
        public String Password { get; set; }
        public String HosptialName { get; set; }
        public String AddTime { get; set; }
        public String Recoder { get; set; }
        public String LastRegTime { get; set; }
        public String HosptialID { get; set; }

        public AdminInfoJSON(String Name, String ID, String Password, String HosptialName, String AddTime, String Recoder, String LastRegTime, String HosptialID)
        {
            this.Name = Name;
            this.ID = ID;
            this.Password = Password;
            this.HosptialName = HosptialName;
            this.AddTime = AddTime;
            this.Recoder = Recoder;
            this.LastRegTime = LastRegTime;
            this.HosptialID = HosptialID;
        }

    }
}